﻿using IntoTheVoid.Models;
using IntoTheVoid.Units;
using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IntoTheVoid.State
{
	public class PreDownloadState : State
	{
		private GameDownloadConfigModel gameDownlaodConifgModel;
		private BaseInfoContentModel baseInfoContentModel;
		public BaseInfoContentModel BaseInfoContentModel { get; private set; }
		private bool isPreDownloadCheck = false;
		public PreDownloadState(StateContext context)
		{
			isPreDownloadCheck = true;
			buttonContent = "  预下载";
			buttonEnabla = false;
			Context = context;
			gameDownlaodConifgModel = GameDownloadConfigModel.Instance;
			baseInfoContentModel = BaseInfoContentModel.Instance;
			gameDownlaodConifgModel.DownLoadConfigCompleted += DownLoadConfigCompleted;

			// 请求数据
			gameDownlaodConifgModel.SendStartConfigMessage(baseInfoContentModel.GetValue<string>("version", ""));
			Start(context);
		}
		public override void Start(StateContext context)
		{
			Context = context;
			Context.OnButtonStateArgs(new ButtonStateChangeEventArgs(buttonContent, buttonEnabla, false, false, "StartDownloadButtonGray"));
			Context.StateType = StateType.PreDownloading;
		}
		private void DownLoadConfigCompleted(object sender, GameDownloadConfigEventArgs e)
		{
			if (!isPreDownloadCheck)
			{
				return;
			}
			isPreDownloadCheck = false;

			LogHelper.Info("PreDownloadState DownLoadConfigCompleted response:" + e.Code);
			
			if (e.Code == -1)
			{
				Context.OnTipsStateArgs(new TipsEventArgs("网络连接失败"));
				buttonEnabla = true;
				// 重新请求数据
				Context.OnButtonStateArgs(new ButtonStateChangeEventArgs(buttonContent, buttonEnabla, false, false, "StartDownloadButtonGray"));
			}
			else
			{
				var httpDownLoadInfo = gameDownlaodConifgModel.DownLoadInfo;
				if (httpDownLoadInfo.UnopenedLatest == "" || httpDownLoadInfo.UnopenedLink == "")
				{
					Context.OnTipsStateArgs(new TipsEventArgs("无预下载"));
					Context.StateType = StateType.CheckingUpdate;
					Context.CurState = new FirstCheckState(Context);
					Context.CurState.Start(Context);
					return;
				}
				else
				{
					var defaultPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Game");
					var path = BaseInfoContentModel.Instance.GetValue<string>("gameInstallPath", defaultPath);
					var name = System.IO.Path.GetFileNameWithoutExtension(httpDownLoadInfo.UnopenedLink);

					if (File.Exists(Path.Combine(path, name+".zip")))
					{
						Context.OnTipsStateArgs(new TipsEventArgs("预下载已完成请勿重复操作"));
					}
					else
					{
						Context.OnTipsStateArgs(new TipsEventArgs("有预下载，是否进行预下载", () => {

							Context.CurState = new DownloadState(Context, true);
							Context.CurState.Start(Context);
						}, () => {
							Context.StateType = StateType.CheckingUpdate;
							Context.CurState = new FirstCheckState(Context);
							Context.CurState.Start(Context);
						}));
					}



					return;
				}
			}
		}


	}
}
